<template>
  <a-card :bordered="false">
    <div class="table-operator">
      <a-button type="primary" @click="detailHandler()" :disabled="this.selectedRowKeys.length != 1">查看</a-button>
      <a-button type="danger" :disabled="this.selectedRowKeys.length == 0" @click="deleteHandler()" v-priv="'ElasticSearch.IndicesLibraryManager.IndexDelete'">删除</a-button>
    </div>
    <s-table
      size="default"
      ref="table"
      rowKey="indicesName"
      :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
      :columns="columns"
      :data="loadData"
      :showPagination="false"
    >
      <a slot="indicesName" slot-scope="text,record" @click="$refs.indexDetailModal.info(record.indicesName)">{{ record.indicesName }}</a>
    </s-table>
    <index-library-detail-modal ref="indexDetailModal" />
  </a-card>
</template>

<script>
import { STable } from '@/components'
import { indexLibraryList, delIndexLibrary } from '@/api/system'
import IndexLibraryDetailModal from '@/views/system/components/IndexLibraryDetailModal'
export default {
  name: 'IndicesLibrary',
  components: {
    STable,
    IndexLibraryDetailModal
  },
  data () {
    return {
      description: '',
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      typeOptions: [],
      selectedRowKeys: [],
      selectedRows: [],
      // 表头
      columns: [
        {
          title: '索引库名',
          dataIndex: 'indicesName',
          scopedSlots: { customRender: 'indicesName' }
        },
        {
          title: '索引库别名',
          dataIndex: 'alias'
        },
        {
          title: '创建时间',
          dataIndex: 'creationDate'
        },
        {
          title: '分片数',
          dataIndex: 'numberOfShards'
        },
        {
          title: '副本数',
          dataIndex: 'numberOfReplicas'
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return indexLibraryList(Object.assign(parameter, {}))
      }
    }
  },
  created () {
  },
  methods: {
    onCreateTimeChange (value, dateString) {
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    detailHandler () {
      if (this.selectedRows.length === 1) {
        this.$refs.indexDetailModal.info(this.selectedRows[0].indicesName)
      }
    },
    handleOk () {
      this.$refs.table.refresh(true)
    },
    deleteHandler () {
      const names = this.selectedRowKeys.join()
      const that = this
      this.$confirm({
        title: '确认删除索引库？',
        onOk () {
          delIndexLibrary(names).then(res => {
            if (res.status === 1) {
              that.handleOk()
              that.$message.success(res.message)
            } else {
              that.$message.error(res.message)
            }
          })
        }
      })
    }
  },
  watch: {
  }
}
</script>
